package com.ruoyi.lm.controller;

import java.util.List;

import com.ruoyi.lm.domain.LmRequire;
import com.ruoyi.lm.service.ILmRequireService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lm.domain.LmRequiretype;
import com.ruoyi.lm.service.ILmRequiretypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 需求分类Controller
 * 
 * @author ruoyi
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/lm/requiretype")
public class LmRequiretypeController extends BaseController
{
    private String prefix = "lm/requiretype";

    @Autowired
    private ILmRequiretypeService lmRequiretypeService;
    @Autowired
    private ILmRequireService lmRequireService;

    @RequiresPermissions("lm:requiretype:view")
    @GetMapping()
    public String requiretype()
    {
        return prefix + "/requiretype";
    }

    /**
     * 查询需求分类列表
     */
    @RequiresPermissions("lm:requiretype:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LmRequiretype lmRequiretype)
    {
        startPage();
        List<LmRequiretype> list = lmRequiretypeService.selectLmRequiretypeList(lmRequiretype);
        for(LmRequiretype temp:list){
            LmRequire lmRequire = new LmRequire();
            lmRequire.setRequireType(temp.getId());
            temp.setRequireCount(lmRequireService.selectLmRequireList(lmRequire).size());
        }
        return getDataTable(list);
    }

    /**
     * 导出需求分类列表
     */
    @RequiresPermissions("lm:requiretype:export")
    @Log(title = "需求分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LmRequiretype lmRequiretype)
    {
        List<LmRequiretype> list = lmRequiretypeService.selectLmRequiretypeList(lmRequiretype);
        ExcelUtil<LmRequiretype> util = new ExcelUtil<LmRequiretype>(LmRequiretype.class);
        return util.exportExcel(list, "requiretype");
    }

    /**
     * 新增需求分类
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存需求分类
     */
    @RequiresPermissions("lm:requiretype:add")
    @Log(title = "需求分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LmRequiretype lmRequiretype)
    {
        return toAjax(lmRequiretypeService.insertLmRequiretype(lmRequiretype));
    }

    /**
     * 修改需求分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        LmRequiretype lmRequiretype = lmRequiretypeService.selectLmRequiretypeById(id);
        mmap.put("lmRequiretype", lmRequiretype);
        return prefix + "/edit";
    }

    /**
     * 修改保存需求分类
     */
    @RequiresPermissions("lm:requiretype:edit")
    @Log(title = "需求分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LmRequiretype lmRequiretype)
    {
        return toAjax(lmRequiretypeService.updateLmRequiretype(lmRequiretype));
    }

    /**
     * 删除需求分类
     */
    @RequiresPermissions("lm:requiretype:remove")
    @Log(title = "需求分类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lmRequiretypeService.deleteLmRequiretypeByIds(ids));
    }
}
